﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class HtmlDeclarationNode : IHtmlNode
    {
        string parentpath;

        int nestLevel = 0;

        int nodeIndex = 0;


        string elementName = null;

        string text;

        string rawAttribute = null;

        public HtmlDeclarationNode(string rawAttribute, string parentPath, int nestLevel, int nodeIndex)
        {
            this.elementName = null;
            this.text = null;
            this.rawAttribute = rawAttribute;

            this.parentpath = parentPath;

            this.nestLevel = nestLevel;

            this.nodeIndex = nodeIndex;
        }

        public HtmlDeclarationNode(string elementName, string rawAttribute, string parentPath, int nestLevel, int nodeIndex)
        {
            this.elementName = elementName;
            this.text = null;
            this.rawAttribute = rawAttribute;

            this.parentpath = parentPath;

            this.nestLevel = nestLevel;

            this.nodeIndex = nodeIndex;
        }

        public string ElementName
        {
            get { return elementName; }
            set { elementName = value; }
        }

        public string Text
        {
            get { return text; }
            set { }
        }

        public string RawAttribute
        {
            get { return rawAttribute; }
            set { rawAttribute = value; }
        }

        public IHtmlNodeCollection Node
        {
            get
            {
                return new HtmlNodeCollection(new List<IHtmlNode>(), parentpath + nodeIndex + StringHelper.default_html_pathspliter, nestLevel + 1);
            }
        }

        public IHtmlNodesCollection Nodes
        {
            get
            {
                return new HtmlNodesCollection(new List<IHtmlNode>(), parentpath + nodeIndex + StringHelper.default_html_pathspliter, nestLevel + 1);
            }
        }

        public string Serialize()
        {
            if (string.IsNullOrEmpty(elementName))
                return string.Format("<{0}>", rawAttribute);

            else
                return string.Format("<{0} {1}>", elementName, rawAttribute);
        }

        public string Serialize(bool indent)
        {
            return Serialize();
        }

        public string SerializeToLayout()
        {
            return string.Format("<node path='{0}' element='{1}' {2}>", parentpath + nodeIndex, elementName, rawAttribute);

        }

        public string GetPlainNodeBody()
        {
            return this.Text;
        }

        public override string ToString()
        {
            return Serialize();
        }

        public bool IsEmpty
        {
            get
            {
                return true;
            }
        }

        public bool IsWellFormatted
        {
            get { return false; }
        }

        public string HtmlId
        {
            get { return null; }
        }

        public string HtmlClass
        {
            get { return null; }
        }

        public string ParentPath
        {
            get { return parentpath; }
            set { parentpath = value; }
        }

        public int NestLevel
        {
            get { return nestLevel; }
            set { nestLevel = value; }
        }

        public int NodeIndex
        {
            get { return nodeIndex; }
            set { nodeIndex = value; }
        }
    }
}
